package com.taptech.doufu.ui.adapter

import android.content.Context
import androidx.recyclerview.widget.RecyclerView

abstract class BaseRecyclerAdappter<T,VH: RecyclerView.ViewHolder>(val mContext: Context) : RecyclerView.Adapter<VH>(){

    var mOnItemClickListener:OnItemClickListener<T>? = null

    var dataList = mutableListOf<T>()

    fun setData(sources: MutableList<T>) {
        if (sources == null) {
            return
        }
        dataList = sources
        notifyDataSetChanged()
    }

    fun addData(sources: MutableList<T>) {
        dataList.addAll(sources)
        notifyDataSetChanged()
    }

    fun addData(source: T) {
        dataList.add(source)
        notifyDataSetChanged()
    }




    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: VH, position: Int) {
        holder.itemView.setOnClickListener {
            mOnItemClickListener?.onClick(dataList[position],position)
        }
    }

    interface OnItemClickListener<in T>{
        fun onClick(item:T,position:Int)
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener<T>) {
        mOnItemClickListener = onItemClickListener
    }

}